/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bt;


import bt.ClientServerSelect;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.LocalDevice;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import ui.SimpleGameCanvas;

/**
 * @author Mathieu
 */
public class Main extends MIDlet implements CommandListener {
    
    private Display display;
    public static  BluetoothDiscovery disc;
    private SimpleGameCanvas sgc;

    public Main() {
         display = Display.getDisplay( this );
        ErrorScreen.init(null, display);

        // Create Discovery Object
        disc = new BluetoothDiscovery( display );
    }
    
    public void startApp() {
   String name;

        // Set UUID
        disc.setServiceUUID( "20000000000010008000006057028C19" );

        // Check if Bluetooth is turned on
        try
        {
            name = LocalDevice.getLocalDevice().getFriendlyName();
        }
        catch( BluetoothStateException e )
        {   // display user notification
            showAlertAndExit( "", "Please switch Bluetooth on!", AlertType.ERROR );
            return;
        }

        // Sets the name how this device is shown to the remote user
        disc.setName( name );

        // Start the UI
        startUI();
    }

    /** Offers the user a selection: Client or Server
     */
    private void startUI()
    {
        // Create new screen object for selecting the role: client or server
        ClientServerSelect select = new ClientServerSelect();

        display.setCurrent( select );
   }

    /**
     * Pause is a no-op since there are no background activities or
     * record stores that need to be closed.
     */
    public void pauseApp()
    {
    }

    /**
     * Destroy should cleanup everything.
     * In the case here there is nothing to cleanup.
     * @param unconditional not used
     */
    public void destroyApp( boolean unconditional )
    {
    }

    /**
     * Displays the string that is passed. Waits for user confirmation
     * and offers possibility to exist the Midlet. Used to show notifications to the user.
     * @param t title string that is displayed to the user.
     * @param s string that is displayed to the user.
     * @param type AlertType: ALARM, CONFIRMATION, ERROR, INFO, WARNING
     */
    private void showAlertAndExit( String t, String s, AlertType type )
    {
        Alert a = new Alert( t, s, null, type );
        a.addCommand( new Command( "Exit", Command.EXIT, 1 ) );
        a.setCommandListener( this );
        display.setCurrent( a );
    }


    /** Terminate Midlet.
     */
    public void Exit()
    {
        destroyApp( false );
        notifyDestroyed();
    }

    /** Respond to commands, including exit.
     * On the exit command, cleanup and notify that the MIDlet has been destroyed.
     * @param c The command.
     * @param s The displayable object. */
    public void commandAction(Command c, Displayable s)
    {
        switch( c.getCommandType() )
        {
            case Command.EXIT:
                // User chose to quit the application
                Exit();
                break;
        }
    }

}
